package test5;

import java.util.ArrayList;
import java.util.Scanner;

public class Goods {
    private int id;//商品编号
    private String name;//商品名称
    private int num;//商品库存
    private double price;//商品价格
    private String description;//商品上架状态
    static Scanner sc = new Scanner(System.in);
    public static ArrayList<Goods> list = new ArrayList<>();
    static {
        list.add(new Goods(1, "电脑", 10, 5000, "上架中"));
        list.add(new Goods(2, "鼠标", 20, 50, "上架中"));
        list.add(new Goods(3, "键盘", 30, 100, "上架中"));
        list.add(new Goods(4, "显卡", 40, 2000, "上架中"));
    }

    public Goods() {
    }


    public Goods(int id, String name, int num, double price, String description) {
        this.id = id;
        this.name = name;
        this.num = num;
        this.price = price;
        this.description = description;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    //计算所选的商品总价
    public void show(int id,int num){
        //当存在时
        boolean flag = false;
        for (Goods goods : list) {
            if (id == goods.getId()){
                System.out.println("您要购买的商品信息如下：");
                System.out.println("编号\t\t名称\t\t价格\t\t库存\t\t商品状况");
                System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t" + goods.getNum() + "\t\t" + goods.getDescription());
                System.out.println("您购买商品总价为：" + num * goods.getPrice());
                goods.setNum(goods.getNum() - num);
                flag = true;
                break;
            }
        }
        if (!flag){
            System.out.println("商品不存在");
        }
        show();
    }
    public void show(){
        System.out.println("编号\t\t名称\t\t价格\t\t库存\t\t商品状况");
        for (Goods goods : list) {
            System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t" + goods.getNum() + "\t\t" + goods.getDescription());
        }
    }
}
